using System;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurvivor
{
	public partial class FloatingTextController : ViewController
	{

		private static FloatingTextController mDefault = null;
		void Start()
		{
			FloatingText.Hide();
		}

		private void Awake()
		{
			mDefault = this;
		}

		private void OnDestroy()
		{
			mDefault = null;
		}

		public static void Play(Vector2 pos, string text, bool critical)
		{
			mDefault.FloatingText.InstantiateWithParent(mDefault.transform)
				.Position(pos.x, pos.y)
				.Self(f =>
				{
					Transform textTrans = f.transform.Find("Text");
					Text textComp = textTrans.GetComponent<Text>();
					textComp.text = text;

					if (critical)
					{
						textComp.color = Color.red;
					}
					ActionKit.Sequence()
						.Lerp(0f, 0.3f, 0.5f, p =>
						{
							f.PositionY(pos.y + p * 0.5f);
							f.LocalScaleX(p);
							f.LocalScaleY(p);
						})
						.Delay(0.2f)
						.Lerp(1f, 0f, 0.3f, p =>
						{
							textComp.ColorAlpha(p);
						}, () =>
						{
							f.DestroySelfGracefully();
						}).Start(textComp);
				}).Show();
		}
	}
}
